package front;

import java.sql.Timestamp;
import java.util.ArrayList;
import be.db_backend;
import mt.middle;

public class Thread {

 	private int threadID;
	private int subForumID;
	
	private String subForumName;
	
	private String name;
 	private int createdBy;
 	private User creator;
 	private Timestamp createdOn;
	private int views;
 	private int replies;
 	private Post latestPost;
 	private Post[] posts;
 	private middle m;
 	
 	public Thread(){
 		this.m = new middle(new db_backend());
 	}
 	
 	public Thread(int threadID, int subForumID,String name,int createdBy, Timestamp createdOn,int views, int replies){
 		this.threadID = threadID;
 		this.subForumID = subForumID;
 		this.views = views;
 		this.replies = replies;
 		this.name = name;
 		this.createdBy = createdBy;
 		this.createdOn = createdOn;
 		
 		this.m = new middle(new db_backend());
 		this.subForumName = getNameOfSubforum();
 		
 		try{
 			this.creator = m.getUser(createdBy);
 		}
		catch(Exception e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
 	}
 	
 	public Thread(int subforumid, String NameOfThread, int userID ) {
 		this.threadID = 0;
 		this.subForumID = subforumid;
 		this.views = 0;
 		this.replies = 0;
 		this.name = NameOfThread;
 		this.createdBy = userID;
 		this.createdOn = new Timestamp(System.currentTimeMillis());
 		this.m = new middle(new db_backend());
 		this.subForumName = getNameOfSubforum();
 		
 		try{
 			this.creator = m.getUser(createdBy);
 		}
		catch(Exception e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
	}

	public void Create(){
		try {
 			int id = m.CreateThread(threadID, subForumID, name, createdBy, createdOn, views, replies);
			this.threadID = id;
		} catch (Exception e) {
			
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
 	}
 	
 	public void Edit(){
 		
 	}
 	
 	public void Delete(){
 		
 		try {
 			m.DeleteThread(threadID);
		} catch (Exception e) {
			
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
 	}
 	
 	public String getNameOfSubforum(){
 		if(subForumName == null || subForumName == "")
 			subForumName = m.getSubForumName(subForumID);
 		return subForumName;
	}
 	
 	public void incrementThreadReplies(){
		m.incrementReplies(threadID);
		replies++;
	}
	
	public void incrementThreadViews(){
		m.incrementViews(threadID);
		views++;
	}

	public ArrayList<Post> getAllPosts(){
		return m.getAllPosts(threadID);
	}
	
 	public String toString(){
		return "threadID: " + threadID + 
				", subForumID: " + subForumID + 
				", name: "  + name + 
				", createdBy: " + createdBy +
				", createdOn: " + createdOn +
				", views: "  + views + 
				", replies: "  + replies;
	}
 	
 	public int getThreadID() {
		return threadID;
	}
	public void setThreadID(int threadID) {
		this.threadID = threadID;
	}
	public int getSubForumID() {
		return subForumID;
	}
	public void setSubForumID(int subForumID) {
		this.subForumID = subForumID;
	}
	public String getName() {
		if(name == null ){//|| name.equals("")){
			name = m.getNameOfThread(threadID);
		}
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(int createdBy) {
		this.createdBy = createdBy;
	}
	public Timestamp getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Timestamp createdOn) {
		this.createdOn = createdOn;
	}
	public int getViews() {
		
		try{
			this.views = m.getNumberOfViewsOnAThread(threadID);
 		}
		catch(Exception e){
			
			System.out.println("Error!");
			e.printStackTrace();
		}

		return views;
	}
	public void setViews(int views) {
		//System.out.println("SetViews!");
		incrementThreadViews();
		/*
		this.views = views;
		try{
			m.setNumberOfViesOnAThread(threadID, views);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		*/
	}
	public int getReplies() {
		return replies;
	}
	public void setReplies(int replies) {
		this.replies = replies;
	}

	public Post getLatestPost() {
		
		try{
 			this.latestPost = m.getLatestPost(threadID);
			
 		}
		catch(Exception e){
			e.printStackTrace();
		}
		return latestPost;
	}

	public void setLatestPost(Post latestPost) {
		this.latestPost = latestPost;
	}

	public User getCreator() {
		return creator;
	}

	public void setCreator(User creator) {
		this.creator = creator;
	}

	public Post[] getPosts() {
		try{
			ArrayList<Post> tmp = new ArrayList<Post>();
			tmp = m.getAllPosts(threadID);
			
			this.posts = new Post[tmp.size()];
			for(int i = 0; i < tmp.size(); i++){
				posts[i]=tmp.get(i);
			}
 		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return posts;
	}

	public void setPosts(Post[] posts) {
		this.posts = posts;
	}

	public String getSubForumName() {
		return subForumName;
	}

	public void setSubForumName(String subForumName) {
		this.subForumName = subForumName;
	}
}
